package client.view;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

/**
 * Created by IntelliJ IDEA.
 * User: sam
 * Date: 16/06/12
 * Time: 11:42
 * To change this template use File | Settings | File Templates.
 */
public class ReportsPanel extends JPanel {

    JSplitPane splitPane;
    BugPanel bugPanel;
    static LogPanel logPanel = new LogPanel();

    public ReportsPanel() {

        setLayout( new GridLayout( 1, 1 ) );

        bugPanel = new BugPanel();
        logPanel = new LogPanel();

        splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);

        splitPane.setOneTouchExpandable(true);
        splitPane.setDividerLocation(300);
        splitPane.setLeftComponent(logPanel);
        splitPane.setRightComponent(bugPanel);

        add(splitPane);
    }

    public static void log(String txt){
        System.out.println(txt);
        //ReportsPanel.log("ReportsPanel.log "+logPanel.output);
        String currText = logPanel.output.getText()+"\n";
        logPanel.output.setText(currText+txt);
    }

    public static void clear(){
        logPanel.output.setText("");
     }

    public class BugPanel extends JPanel {

        JButton sendBtn;

        public BugPanel() {

            setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
            //setLayout(new GridLayout(5,1));

            Border bevelBorder = BorderFactory.createBevelBorder(BevelBorder.LOWERED);


            URL resourceURL = getClass().getClassLoader().getResource(MFWindow.MEDIA+"send_log_icon.png");
            Icon tmpIcon = new ImageIcon(resourceURL);

            sendBtn = new JButton("Log Issue");
            sendBtn.setAlignmentX(JLabel.CENTER_ALIGNMENT);
            sendBtn.setIcon(tmpIcon);
            JPanel titlePanel = new JPanel();
            //titlePanel.setLayout(new BoxLayout(titlePanel, BoxLayout.Y_AXIS));

            JLabel titleLabel = new JLabel("Bug title:");
            //titleLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
            JTextArea titleText = new JTextArea();
            titleText.setBorder(bevelBorder);
            titleText.setPreferredSize(new Dimension(270, 20));
            //titleText.setAlignmentX(JLabel.LEFT_ALIGNMENT);
            titlePanel.add(titleLabel);
            //titlePanel.add(Box.createRigidArea(new Dimension(10,5)));
            titlePanel.add(titleText);

            JPanel stepsPanel = new JPanel();
            JLabel stepsLabel = new JLabel("Steps to replicate:");
            JTextArea stepsText = new JTextArea();
            stepsText.setBorder(bevelBorder);
            stepsText.setPreferredSize(new Dimension(270, 60));
            stepsPanel.add(stepsLabel);
            stepsPanel.add(stepsText);

            JPanel descPanel = new JPanel();
            JLabel descLabel = new JLabel("Bug description:");
            JTextArea descText = new JTextArea();
            descText.setBorder(bevelBorder);
            descText.setPreferredSize(new Dimension(270, 60));
            descPanel.add(descLabel);
            descPanel.add(descText);

            JLabel bugHeaderLabel = new JLabel("Report a Bug");
            bugHeaderLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);

            resourceURL = getClass().getClassLoader().getResource(MFWindow.MEDIA+"bug_icon.png");
            tmpIcon = new ImageIcon(resourceURL);

            bugHeaderLabel.setIcon(tmpIcon);
            bugHeaderLabel.setFont(new Font(bugHeaderLabel.getFont().getName(), Font.BOLD, bugHeaderLabel.getFont().getSize() + 1));

            add(Box.createRigidArea(new Dimension(10,10)));
            add(bugHeaderLabel);
            add(Box.createRigidArea(new Dimension(10,10)));
            add(titlePanel);
            add(stepsPanel);
            add(descPanel);
            add(Box.createRigidArea(new Dimension(10,10)));
            add(sendBtn);
            add(Box.createRigidArea(new Dimension(10,10)));
        }
    }

    public static class LogPanel extends JPanel implements ActionListener {

        JEditorPane output = new JEditorPane();
        JScrollPane scrollPane;
        JButton sendBtn;

        public LogPanel() {

            setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

            Font logFont = new Font("Courier", Font.BOLD, 10);

            URL resourceURL = getClass().getClassLoader().getResource(MFWindow.MEDIA+"send_log_icon.png");
            Icon tmpIcon = new ImageIcon(resourceURL);

            sendBtn = new JButton("Report logs to Music-alms");
            sendBtn.addActionListener(this);
            sendBtn.setIcon(tmpIcon);
            sendBtn.setAlignmentX(JLabel.CENTER_ALIGNMENT);    // if i dont have this, then label header alignment does not work

            JPanel outputPanel = new JPanel();

            output = new JEditorPane();
            output.setFont(logFont);
            //output.setBackground(new Color(255,250,205));
            output.setBackground(new Color(76,76,76));
            output.setForeground(new Color(255,255,255));
            output.setEditable(false);
            //output.setContentType("text/html");       // prevents output showing

            Dimension logDimension = new Dimension(290, 235);
            output.setPreferredSize(logDimension);

            scrollPane = new JScrollPane(output, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            scrollPane.getVerticalScrollBar().setUnitIncrement(16);
            scrollPane.setPreferredSize(logDimension);

            JLabel logHeaderLabel = new JLabel("Log");
            logHeaderLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);

            resourceURL = getClass().getClassLoader().getResource(MFWindow.MEDIA+"book_icon.png");
            tmpIcon = new ImageIcon(resourceURL);

            logHeaderLabel.setIcon(tmpIcon);
            logHeaderLabel.setFont(new Font(logHeaderLabel.getFont().getName(), Font.BOLD, logHeaderLabel.getFont().getSize() + 1));

            outputPanel.add(logHeaderLabel);
            outputPanel.add(Box.createRigidArea(new Dimension(10, 10)));
            outputPanel.add(scrollPane);
            outputPanel.add(Box.createRigidArea(new Dimension(10, 10)));
            //outputPanel.add(Box.createRigidArea(new Dimension(10,10)));
            //outputPanel.add(sendBtn);
            //outputPanel.add(Box.createRigidArea(new Dimension(10,10)));

            add(outputPanel);

            // add these to main JPanel container, otherwise y-axis alignment doesnt have same padding.
            add(Box.createRigidArea(new Dimension(10,10)));
            add(sendBtn);
            add(Box.createRigidArea(new Dimension(10, 10)));

        }

        public void actionPerformed(ActionEvent actionEvent) {

            String strCommand = actionEvent.getActionCommand();

            ReportsPanel.log("ReportsPanel.actionPerformed() " + strCommand);

            if(strCommand.equals("Report logs to Music-alms")) {

            }
        }
    }
}
